package com.mstar.qs.common;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.utils.URIUtils;

/**
 * URL example:
 * 
 * <code>
 * http://msdxml.morningstar.com/index.php?username=name&password=pwd&instruments=126.1.IBM&timesale
 * </code>
 * 
 * @author dchen2
 * 
 */
public class URLUtil {

	private String protocol = "http";
	private String host;
	private int port = -1;
	private String path = null;

	/**
	 * Generate url with query string and individual parameters
	 * 
	 * http://msdxml.morningstar.com/index.php?username=name&password=pwd&
	 * instruments=126.1.IBM&timesale
	 * 
	 * before call this method, properties host and path must be set.
	 * 
	 * @param queryQarams
	 *            consisted of key value pairs. taking an example of
	 *            username=name&password=pwd&instruments=126.1.IBM
	 * @param extParam
	 *            individual parameters. for instance: &timesale
	 * @return
	 * @throws URISyntaxException
	 */
	public URI assembleUri(LinkedHashMap<String, String> queryQarams,
			Set<String> extParam) throws URISyntaxException {
		return assembleUri(this.protocol, this.host, this.port, this.path,
				queryQarams, extParam, null);
	}

	public URI assembleUri(LinkedHashMap<String, String> queryQarams)
			throws URISyntaxException {
		return assembleUri(this.protocol, this.host, this.port, this.path,
				queryQarams, null, null);
	}

	/**
	 * before calling this method, host must be set value
	 * 
	 * @param path
	 *            such as index.php
	 * @param queryQarams
	 * @return
	 * @throws URISyntaxException
	 */

	public URI assembleUri(String path,
			LinkedHashMap<String, String> queryQarams)
			throws URISyntaxException {
		return assembleUri(this.protocol, this.host, this.port, path,
				queryQarams, null, null);
	}

	public URI assembleUri(String path,
			LinkedHashMap<String, String> queryQarams, Set<String> extParams)
			throws URISyntaxException {
		return assembleUri(this.protocol, this.host, this.port, path,
				queryQarams, extParams, null);
	}
	public URI assembleUri(String host, String path,
			LinkedHashMap<String, String> queryQarams)
			throws URISyntaxException {
		return assembleUri(this.protocol, host, this.port, path, queryQarams,
				null, null);
	}

	/**
	 * 
	 * @param url
	 *            it should be like http://msdxml.morningstar.com/index.php
	 * @param queryQarams
	 * @return
	 * @throws URISyntaxException
	 */
	public URI assembleWithSemiFinishedGoods(String url,
			LinkedHashMap<String, String> queryQarams)
			throws URISyntaxException {
		return assembleWithSemiFinishedGoods(url, queryQarams,null);
	}

	/**
	 * 
	 * @param url
	 *            It should be like http://msdxml.morningstar.com/index.php
	 * @param path
	 * @param queryQarams
	 * @return
	 * @throws URISyntaxException
	 */
	public URI assembleWithSemiFinishedGoods(String url, String path,
			LinkedHashMap<String, String> queryQarams)
			throws URISyntaxException {
		String u = null;
		if (url.endsWith("/") || path.startsWith("/"))
			u = url + path;
		else
			u = url + "/" + path;
		return assembleWithSemiFinishedGoods(u, queryQarams);
	}

	/**
	 * 
	 * @param url
	 *            it should be like http://msdxml.morningstar.com/index.php
	 * @param queryQarams
	 * @param extParam
	 * @return
	 * @throws URISyntaxException
	 */
	public URI assembleWithSemiFinishedGoods(String url,
			LinkedHashMap<String, String> queryQarams, Set<String> extParam)
			throws URISyntaxException {
		String q = generateQuery(queryQarams, extParam);
		if (q.length() > 0) {
			if (!url.contains("?")) {
				url += "?";
			} else
				url += "&";
			return new URI(url += q);
		}
		return new URI(url);
	}

	/**
	 * 
	 * @param protocal
	 *            http or https
	 * @param host
	 *            it should be like msdxml.morningstar.com
	 * @param port
	 *            it should be like index.php or .../.../index.jsp
	 * @param path
	 * @param queryQarams
	 * @param extParam
	 * @param fragment
	 * @return
	 * @throws URISyntaxException
	 */
	public static URI assembleUri(String protocal, String host, int port,
			String path, LinkedHashMap<String, String> queryQarams,
			Set<String> extParam, String fragment) throws URISyntaxException {
		URI uri = URIUtils.createURI(protocal, host, port, path,
				generateQuery(queryQarams, extParam), fragment);
		return uri;
	}

	public static String generateQuery(
			LinkedHashMap<String, String> queryQarams, Set<String> extParam) {

		StringBuilder sb = new StringBuilder();
		if (queryQarams != null) {
			for (Map.Entry<String, String> entry : queryQarams.entrySet()) {
				if (sb.length() > 0)
					sb.append("&");
				sb.append(entry.getKey()).append("=").append(entry.getValue());
			}
		}
		if (extParam != null) {
			for (String s : extParam) {
				if (sb.length() > 0)
					sb.append("&");
				sb.append(s);
			}
		}
		return sb.toString();
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
